<template>
  <div class="drawer" :style="drawerStyle">
    <div class="drawer__title">
      <span>{{ title }}</span>
      <span @click="visible = false">X</span>
    </div>
    <div class="drawer__content">
      <slot></slot>
    </div>
  </div>
</template>

<script>
import { ref, watch, computed } from "vue";

export default {
  name: "RDrawer",
  props: {
    modelValue: Boolean,
    title: String,
    width: String,
  },
  setup(props, ctx) {
    const visible = ref(props.modelValue);

    watch(visible, (newVal) => {
      ctx.emit("update:modelValue", newVal);
    });
    watch(
      () => props.modelValue,
      (newVal) => {
        visible.value = newVal;
      }
    );
    const drawerStyle = computed(() => {
      return {
        width: props.width,
        left: `${visible.value ? 0 : -parseInt(props.width) - 10}px`,
      };
    });
    return {
      visible,
      drawerStyle,
    };
  },
};
</script>

<style scoped lang="less">
@padding: 15px;
@boxType: border-box;
.drawer {
  position: fixed;
  top: 0;
  height: 100%;
  padding: @padding;
  box-sizing: @boxType;
  background-color: #fff;
  transition: left 1s;
  z-index: 99;
  box-shadow: 5px 0 10px lightgrey;

  @titleHeight: 50px;

  &__title {
    padding: @padding;
    box-sizing: @boxType;
    position: absolute;
    left: 0;
    top: 0;
    width: 100%;
    height: @titleHeight;
    display: flex;
    flex-direction: row;
    justify-content: space-between;
    align-items: center;
    font-size: 15px;
    font-weight: bold;
    border-bottom: 1px solid lightgrey;

    span:last-child {
      cursor: pointer;
    }
  }

  &__content {
    margin-top: (@titleHeight - 15px);
  }
}
</style>
